package com.yowasa.xiuxian.engine.message.map;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.ExploreBattleLog;
import com.yowasa.xiuxian.entity.po.XiuXianExploreUser;
import com.yowasa.xiuxian.entity.po.XiuxianMapInfoPO;
import com.yowasa.xiuxian.entity.po.XiuxianPrayerLog;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.*;
import com.yowasa.xiuxian.util.ComponentUtil;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CMD(name = "/探索", scope = CmdScopeEnum.ALL)
public class ExploreHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private ExploreHelper exploreHelper;


    /**
     * 鸣神岛   荒海  影向山  神里屋敷  镇守之森  白狐之野  绀田村
     * 神无冢   九条阵屋  踏鞴砂  名椎滩
     * 清籁岛   浅濑神社  清籁丸  越石村  平海砦  天云峠
     * 鹤观     知比山  菅名山   逢岳之野   惑饲滩  笈名海滩  千来神祠
     * 八酝岛   无想刃狭间  蛇神之首  蛇骨矿洞  无名砦  藤兜砦  绯木村
     * 海祇岛   珊瑚宫   水月池  曚云神社  望泷村
     */
    @Override
    @Transactional
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUserAndState(message);
        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可追加二级指令 进入 离开 我的 战斗记录");
            return;
        }
        XiuXianExploreUser exploreUser = exploreHelper.selectUser(userBO.getId());
        if("进入".equals(params.get(0))){
            userHelper.checkCd(userBO);
            if(exploreUser.getStatus() == 0){
                //可以进入，判断地点
                if(userBO.getLocal() < 20 ){
                    throw new HandlerException("此处无法进行探索");
                }
                //进入
                exploreUser.setStatus(1);
                exploreUser.setExploreMap(userBO.getLocal());
                exploreHelper.updateUser(exploreUser);

                userBO.setLockEndTime(DateXUtil.getMaxTime());
                userBO.setLockReason(userBO.getName()+"正在探索中,请先结束探索");
                userHelper.saveUserInfo(userBO);

                List<List<MysComponent>> lists = showherePanel(userBO);
                messageHelper.replyMysPanel(message,"探索",null,lists,null);

            }
        }
        if("离开".equals(params.get(0))){
            if(exploreUser.getStatus() == 0){
                //当前不在探索状态，无须离开
                throw new HandlerException("当前没有在探索中哦");
            }
            exploreHelper.checkCd(exploreUser);
            exploreUser.setStatus(0);
            exploreHelper.updateUser(exploreUser);
            userBO.setLockEndTime(new Date());
            userHelper.saveUserInfo(userBO);
            messageHelper.replyMys(message,"已成功从探索状态脱离");
        }
        if("我的".equals(params.get(0))) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("雷之印数量 ： ").append(exploreUser.getGoldNum())
                    .append("\n探索状态： ").append(exploreUser.getStatus() == 1 ? "探索中":"空闲中")
                    .append("\n探索地点： ").append(exploreUser.getMapName())
                    .append("\n业力值： ").append(exploreUser.getKarma());
            messageHelper.replyMys(message,stringBuffer.toString());
            if(exploreUser.getStatus() == 1){
                List<List<MysComponent>> lists = showherePanel(userBO);
                messageHelper.replyMysPanel(message,"探索",null,lists,null);
            }
        }
        if("战斗记录".equals(params.get(0))) {
            List<ExploreBattleLog> battleLogs = new ArrayList<>();
            if(params.size() == 1){
                battleLogs = exploreHelper.selectSize(userBO.getId(),0);
            }else {
                int i;
                try {
                    i = Math.abs(Integer.parseInt(params.get(1)));
                }catch (Exception e){
                    i=1;
                }
                battleLogs = exploreHelper.selectSize(userBO.getId(),i-1);
            }
            StringBuffer sb = new StringBuffer();
            sb.append("\n默认一次查询10条记录，可使用 /探索 战斗记录 页码  指定查询第几页\n\n");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            battleLogs.forEach(e -> {
                sb.append(sdf.format(e.getCreateTime())).append("   ").append(e.getName()).append(e.getBattleResult() == -1 ? "偷袭你但是被你反杀了，你反而抢走了他" : "偷袭你并抢走了").append(e.getGoldNum()).append("个雷之印").append("\n");
            });
            messageHelper.replyMys(message,sb.toString());
        }

    }

    private List<List<MysComponent>> showherePanel(UserBO userBO) {

        List<XiuxianMapInfoPO> mapList = exploreHelper.selectMapInfoByParent(userBO.getLocal());

        List<MysComponent> components = new ArrayList<>();
        for (int i = 0 ;i < mapList.size();i++){
            MysComponent component = new MysComponent((i+1)+"",1,mapList.get(i).getName(),"111");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key","探索地点");
            jsonObject.put("mapId",mapList.get(i).getId());
            component.setExtra(jsonObject.toString());
            components.add(component);
        }
        MysComponent[] mysComponents = components.toArray(new MysComponent[]{});
        List<List<MysComponent>> lists = ComponentUtil.genComponentList(2, mysComponents);
        return lists;
    }


    public String desc(){
        return new StringBuffer().append("进入地图探索\n")
                .toString();

    }

}
